﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Grundy.Common;
using Grundy.CryptographicServices;
using Grundy.Message;
using Grundy.TestHelpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;

namespace Grundy.Entity.Tests
{
    [TestClass]
    public class LicenseManagerTests
    {
        private CryptographicServicesProvider cryptographicServicesProvider;
        private ObjectMother _objectMother = new ObjectMother();

        [TestInitialize]
        public void Setup()
        {
            var provider = new RSACryptoServiceProvider();
            var aliceKeyPair = provider.ToXmlString(true);
            var alicePublicKey = provider.ToXmlString(false);
            cryptographicServicesProvider = new RsaProvider(aliceKeyPair);
        }

        [TestMethod]
        public void Should_Create_License_For_Entitlement()
        {
            var customerEntitlement = _objectMother.GetCustomerEntitlement(new List<LicenseEntitlement>
                                                                 {
                                                                     _objectMother.GetLicenseEntitlement(_objectMother.GetProduct("Product Name 1", new [] {"Feature1", "Feature2"}),_objectMother.GetCountedFloatingLicense(12345)),
                                                                     _objectMother.GetLicenseEntitlement(_objectMother.GetProduct("Product Name 2", new string[] {"Feature1", "Feature2"}), _objectMother.GetNodeLockedLicense())
                                                                 }, _objectMother.GetCustomer("Ashish Sharma"));
            var licenseManager = new LicenseManager(cryptographicServicesProvider);
            var license = licenseManager.CreateLicenseForEntitlement(customerEntitlement);

            license.Payload.Should().NotBeBlank();
            license.Signature.Should().NotBeBlank();
            license.Provider.Should().NotBeBlank();
            license.Algorithm.Should().NotBeBlank();
        }

        [TestMethod]
        public void Should_Create_Entitlement_For_License()
        {
            var customerEntitlement = _objectMother.GetCustomerEntitlement(new List<LicenseEntitlement>
                                                                 {
                                                                     _objectMother.GetLicenseEntitlement(_objectMother.GetProduct("Product Name 1", new [] {"Feature1", "Feature2"}),_objectMother.GetCountedFloatingLicense(12345)),
                                                                     _objectMother.GetLicenseEntitlement(_objectMother.GetProduct("Product Name 2", new [] {"Feature1", "Feature2"}), _objectMother.GetNodeLockedLicense())
                                                                 }, _objectMother.GetCustomer("Ashish Sharma"));
            var licenseManager = new LicenseManager(cryptographicServicesProvider);
            var licenseForEntitlement = licenseManager.CreateLicenseForEntitlement(customerEntitlement);

            var entitlement = licenseManager.CreateCustomerEntitlementFromLicense(licenseForEntitlement);
            entitlement.Should().NotBeNull();
            entitlement.Customer.Name.Should().Be("Ashish Sharma");
            entitlement.LicenseEntitlements.Count().Should().Be(2);
        }

    }
}
